#!/bin/bash

unalias gcmsg &>/dev/null || true
function gcmsg() {
  git commit -m "$*"
}

unalias gp &>/dev/null || true
function gp() {
    declare branch origin param
    declare -a gp_opts=()
    while param="${1:-}"; [[ -n "$param" ]]; do
        [[ "$param" == -* ]] || break
        shift
        gp_opts+=("$param")
    done
    origin="${1:-$(git config --get branch.master.remote)}" || return 1
    branch="${2:-$(git rev-parse --abbrev-ref HEAD)}" || return 1
    if ! git rev-parse --abbrev-ref --symbolic-full-name '@{u}' &>/dev/null; then
        gp_opts+=(-u)
    fi
    gp_opts+=("$origin" "$branch")
    echo git push "${gp_opts[@]}"
    git push "${gp_opts[@]}"
}

function gpr() {
    gp "$@"
    git log -1 --pretty="${GPPR_PREFIX:+$GPPR_PREFIX }%B" |
    hub pull-request -F-
}

function gpw() {
    GPPR_PREFIX="WIP" gpr "$@"
}

function ff() {
    declare -a ff_opts=(-name "${1:+*$1}*");
    [[ "$#" -ge 1 ]] && shift
    for name; do
        ff_opts+=(-or -name "*${name}*")
    done
    find . -type f "${ff_opts[@]}"
}
